package com.darren.euc.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonFormat;

import net.sf.json.JSONArray;

@Entity
@Table(name = "euc_room")
public class Room implements Serializable {

	private static final long serialVersionUID = 8419897234136199253L;

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	@Column(name = "room_name", nullable = true, columnDefinition = "varchar", length = 255)
	private String roomName;

	@ManyToOne(targetEntity = RoomType.class, fetch = FetchType.EAGER)
	@JoinColumn(name = "room_type_id", referencedColumnName = "id")
	private RoomType roomType;

	@ManyToOne(targetEntity = Province.class, fetch = FetchType.EAGER)
	@JoinColumn(name = "province_id", referencedColumnName = "province_id")
	private Province province;

	@ManyToOne(targetEntity = City.class, fetch = FetchType.EAGER)
	@JoinColumn(name = "city_id", referencedColumnName = "city_id")
	private City city;
	
	

//	@ManyToOne(targetEntity = Space.class, fetch = FetchType.EAGER)
//	@JoinColumn(name = "space_id", referencedColumnName = "id")
//	private Space space;
	
	@ManyToOne
	@JoinColumn(name="space_id", referencedColumnName = "id") 
	@Basic(fetch=FetchType.LAZY)
	private Space space;

	@Column(name = "room_price", nullable = true)
	private Double roomPrice;

	@Column(name = "seat_count", nullable = true)
	private Integer seatCount;
	
	/**
	 * 房间灯的硬件设备编号
	 */
	@Column(name = "friend", nullable = true)
	private String friend;
	
	/**
	 * 房间灯的名称，用分号 ';' 隔开
	 */
	@Column(name = "light_names", nullable = true)
	private String lightNames;		

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	@Column(name = "create_time", nullable = true, columnDefinition = "datetime")
	private Date createTime;

	@ManyToOne(targetEntity = User.class, fetch = FetchType.EAGER)
	@JoinColumn(name = "create_user_id", referencedColumnName = "user_id")
	private User createUser;

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	@Column(name = "last_modify_time", nullable = true, columnDefinition = "datetime")
	private Date lastModifyTime;

	@ManyToOne(targetEntity = User.class, fetch = FetchType.EAGER)
	@JoinColumn(name = "last_modify_user_id", referencedColumnName = "user_id")
	private User lastModifyUser;

	@Column(name = "enable", nullable = false, columnDefinition = "int default 1")
	private boolean enable = true;
	
	@Transient
	private JSONArray lights = new JSONArray();
	

	@OneToMany(cascade=CascadeType.REFRESH)
    @JoinColumn(name="room_id")   
    @OrderBy(value = "rentStart desc") 
	private List<ContractProduct> contractProducts = new ArrayList<ContractProduct>();

	
	public List<ContractProduct> getContractProducts() {
		return contractProducts;
	}

	public void setContractProducts(List<ContractProduct> contractProducts) {
		this.contractProducts = contractProducts;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getRoomName() {
		return roomName;
	}

	public void setRoomName(String roomName) {
		this.roomName = roomName;
	}

	public RoomType getRoomType() {
		return roomType;
	}

	public void setRoomType(RoomType roomType) {
		this.roomType = roomType;
	}

	public Province getProvince() {
		return province;
	}

	public void setProvince(Province province) {
		this.province = province;
	}

	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}

	public Space getSpace() {
		return space;
	}

	public void setSpace(Space space) {
		this.space = space;
	}

	public Double getRoomPrice() {
		return roomPrice;
	}

	public void setRoomPrice(Double roomPrice) {
		this.roomPrice = roomPrice;
	}

	public Integer getSeatCount() {
		return seatCount;
	}

	public void setSeatCount(Integer seatCount) {
		this.seatCount = seatCount;
	}
	
	public String getFriend() {
		return friend;
	}

	public void setFriend(String friend) {
		this.friend = friend;
	}

	public String getLightNames() {
		return lightNames;
	}

	public void setLightNames(String lightNames) {
		this.lightNames = lightNames;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public User getCreateUser() {
		return createUser;
	}

	public void setCreateUser(User createUser) {
		this.createUser = createUser;
	}

	public Date getLastModifyTime() {
		return lastModifyTime;
	}

	public void setLastModifyTime(Date lastModifyTime) {
		this.lastModifyTime = lastModifyTime;
	}

	public User getLastModifyUser() {
		return lastModifyUser;
	}

	public void setLastModifyUser(User lastModifyUser) {
		this.lastModifyUser = lastModifyUser;
	}

	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	public JSONArray getLights() {
		return lights == null?new JSONArray():lights;
	}

	public void setLights(JSONArray lights) {
		this.lights = lights;
	}
}
